package com.lianyu.wangyiyun.fragment;

import android.annotation.SuppressLint;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.lianyu.wangyiyun.activity.PlayerActivity;
import com.lianyu.wangyiyun.databinding.FragemntLyricBinding;
import com.lianyu.wangyiyun.model.Mp3Info;
import com.lianyu.wangyiyun.model.MusicListViewModel;
import com.lianyu.wangyiyun.utils.MyRetrofit;
import com.lianyu.wangyiyun.widget.lrcview.LrcView;

import java.io.File;
import java.util.List;

/**
 * @author Chen Yuguang
 * @date 2021/5/9 16:46
 */
public class LyricFragment extends AbstractLazyFragment {

    private FragemntLyricBinding fragemntLyricBinding;

    private long lastTime;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        fragemntLyricBinding = FragemntLyricBinding.inflate(getLayoutInflater());
        fragemntLyricBinding.lrcView.setOnTapListener(new LrcView.OnTapListener() {
            @Override
            public void onTap(LrcView view, float x, float y) {
                PlayerActivity playerActivity = (PlayerActivity) getActivity();
                playerActivity.switchPager();
            }
        });
        MusicListViewModel mainMusicListViewModel = new ViewModelProvider(getActivity(), new ViewModelProvider.NewInstanceFactory()).get(MusicListViewModel.class);
        mainMusicListViewModel.getPositionLiveData().observe(this, position -> {
            List<Mp3Info> mp3Infos = mainMusicListViewModel.getMusicLiveData().getValue();
            if (mp3Infos != null) {
                Mp3Info mp3Info = mp3Infos.get(position);
                String url = mp3Info.getUrl();
                String http = "http";
                if (new File(url).isFile()) {
                    //本地文件加载空歌词
                    fragemntLyricBinding.lrcView.loadLrc("");
                } else if (url.startsWith(http)) {
                    long id = mp3Info.getId();
                    //根据id获取网络歌词
                    MyRetrofit.get3(this, id);
                } else {
                    //特殊情况加载空歌词
                    fragemntLyricBinding.lrcView.loadLrc("");
                }
            }
        });
        fragemntLyricBinding.lrcView.setDraggable(true, (view, time) -> {
            PlayerActivity playerActivity = (PlayerActivity) getActivity();
            playerActivity.seekTo((int) time);
            return true;
        });
        return fragemntLyricBinding.getRoot();
    }

    /**
     * 请求网络获取歌词
     *
     * @param mainLyric 主歌词
     * @param tLyric    翻译后的歌词
     * @author Chen Yuguang
     * @date 2021/5/31 6:24
     */
    public void updateLyric(String mainLyric, String tLyric) {
        if (fragemntLyricBinding.lrcView != null) {
            //避免翻译歌词为空的情况
            if (tLyric.isEmpty()) {
                tLyric = null;
            }
            fragemntLyricBinding.lrcView.loadLrc(mainLyric, tLyric);
        }
    }

    /**
     * 请求网络获取歌词
     *
     * @param time 刷新歌词的时间进度
     * @author Chen Yuguang
     * @date 2021/5/31 6:24
     */
    public void updateTime(int time) {
        if (fragemntLyricBinding.lrcView != null) {
            fragemntLyricBinding.lrcView.updateTime(time);
        }
    }

}
